package com.jastar.android.game.cannon.model;

import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;

public class Obstacle extends CollidableEntity {
    private final Paint paint;
    private float w, h;

    public Obstacle(float x, float y, float w, float h, int weight) {
        super(x, y, weight);
        this.w = w;
        this.h = h;
        paint = new Paint();
        paint.setColor(0xFF666666);
    }

    @Override
    public boolean checkCollision(float x, float y, float r) {
        float closestX = Math.max(this.x - w / 2, Math.min(x, this.x + w / 2));
        float closestY = Math.max(this.y - h / 2, Math.min(y, this.y + h / 2));

        float dx = x - closestX;
        float dy = y - closestY;
        float distance = (float) Math.sqrt(dx * dx + dy * dy);

        return distance < r;
    }

    @Override
    public void draw(Canvas canvas) {
        RectF rect = new RectF(x - w / 2, y - h / 2, x + w / 2, y + h / 2);
        canvas.drawRect(rect, paint);
    }

}